package cn.xfdzx.android.apps.shop.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.util.Utils;


/**
 * Creator: zf.
 * Date: 2019/4/1.
 * Description: 更新版本dialog.
 */
public class UpdateVersionDialog extends Dialog {

    private Context context;
    private OnListenerInterface clickListenerInterface;
    private TextView tv_updateafter,tv_updatenow;//稍后更新，立即更新
    private TextView tv_text;//更新说明
    private TextView tv_version;//版本号
    private ImageView img_clsoe;
    private String versionName;
    private String updateDes;
    private String updateType = "";//判断是否强制更新1不强制2强制
   // private List<FirstDateModel.AndroidUpdateInfoListBean> androidUpdateInfoList;
   // private RecyclerView recycle_updateinfo;//
    //private UpdateInfoListAdapter updateInfoListAdapter;

     public UpdateVersionDialog(Context context, String versionName, String updateDes,
                                String  updateType) {
        super(context, R.style.CustomDialog_Translucent);
        this.context = context;
        this.versionName = versionName;
        this.updateDes = updateDes;
        this.updateType = updateType;
     }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false); // 点击空白区域可以Dismiss对话框
        setCancelable(false); // 点击返回按键可以Dismiss对话框
        init();
      }

    public void init() {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_updateversion, null);
        setContentView(view);
        tv_updatenow =  view.findViewById(R.id.tv_updatenow);
        tv_version = view.findViewById(R.id.tv_versionnum);
        tv_text = view.findViewById(R.id.tv_text);
        img_clsoe = view.findViewById(R.id.img_clsoe);
        tv_text.setText(updateDes);
        tv_version.setText("v"+versionName);

        tv_updatenow.setOnClickListener(new clickListener());
        img_clsoe.setOnClickListener(new clickListener());
//        LinearLayoutManager layoutManager_alllog = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false);
//        recycle_updateinfo.setLayoutManager(layoutManager_alllog);
//        updateInfoListAdapter = new UpdateInfoListAdapter(androidUpdateInfoList);
//        recycle_updateinfo.setAdapter(updateInfoListAdapter);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT; // 高度设置为屏幕的0.6
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

        if (updateType.equals("1")){//选择更新
            img_clsoe.setVisibility(View.VISIBLE);
        }else {
            img_clsoe.setVisibility(View.INVISIBLE);
        }
    }

    public void setClicklistener(OnListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }
    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.tv_updatenow:
                    if (Utils.isFastClick()){
                    clickListenerInterface.doConfirm();}
                    break;
                case R.id.img_clsoe:
                    if (Utils.isFastClick()){
                    clickListenerInterface.doCancel();}
                    break;
            }
        }
    }

    public interface OnListenerInterface {

         void doConfirm();

         void doCancel();
        }
}
